package com.plum.netty.demo;

import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.NettyRuntime;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j
public class TestEventLoop {
    public static void main(String[] args) {
        // 1. 创建事件循环组
        EventLoopGroup group = new NioEventLoopGroup(); //io 事件，普通任务，定时任务
        //EventLoopGroup group = new DefaultEventLoopGroup(); //普通任务，定时任务
        // 2. 获取下一个事件循环对象
        System.out.println(group.next());
        // 3. 执行普通任务
        group.next().submit(() -> {
            try {
                Thread.sleep(1000);
            }catch (Exception e){
                e.printStackTrace();
            }
            log.debug("ok");
        });
        log.debug("main");
        // 4. 执行定时任务
        group.next().scheduleAtFixedRate(() -> {
            log.debug("ok");
        },1,2, TimeUnit.SECONDS);

        //关闭 group
        group.shutdownGracefully();
    }
}

//        19:03:55.133 [main] DEBUG com.plum.netty.demo.TestEventLoop - main
//        19:03:56.134 [nioEventLoopGroup-2-1] DEBUG com.plum.netty.demo.TestEventLoop - ok
//        19:03:56.134 [nioEventLoopGroup-2-2] DEBUG com.plum.netty.demo.TestEventLoop - ok
//        19:03:58.137 [nioEventLoopGroup-2-2] DEBUG com.plum.netty.demo.TestEventLoop - ok

